<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            font: 12px;
            text-align: center;
        }
        
        .f {
            width: 50px;
            border: 1px solid #ccc;
            font-family: inherit;
            padding: 3px;
            margin-right: 10px;
        }
    </style>
</head>

<body>
    <input type="text" class="f" /><input type="button" value="查询" />
</body>
<script>
    var fds = document.getElementsByTagName("input");
    var axp = document.getElementsByTagName("span")[0];
    var i = 0;

    fds[0].onkeyup = function() {
        this.value = this.value.replace(/[^\d]/, "")
    }

    fds[1].onclick = function() {
        (fds[0].value == "") ?
        alert("请输入数字！"):
            alert(/^\d{2}$/.test(parseInt(fds[0].value)) ? "两位" : "这是" + fds[0].value.length + "位数");
    }
</script>

</html>